package com.zdy.showgoods;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.networklibrary.SharedPreferencesUtils;
import com.example.networklibrary.config.KeyInterface;
import com.example.networklibrary.model.GuestModel;
import com.example.networklibrary.model.RFID;
import com.example.networklibrary.model.UserInfo;
import com.zdy.service.RFIDReadService;
import com.zdy.modellibrary.base.BaseActivity;
import com.zdy.utils.AppUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;


public class ActivityMain extends BaseActivity implements View.OnClickListener {
    private UserInfo userStaffInfo;
    private ImageView main_user_img;
    private TextView main_user_name;
    private TextView main_user_code;
    private Button main_guest_login;
    private Button main_callback;
    private Button main_logout;
    private TextView main_guest;
    private TextView versionName;
    private String productRFID = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        GuestModel guestModel = new GuestModel();
        guestModel.setCustomer_id("0");
        guestModel.setCustomer_name("游客");
        SharedPreferencesUtils.setString(this, KeyInterface.GuestInfo, JSONObject.toJSONString(guestModel));
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        productRFID = "";
        initGuestInfo();
    }

    private void initGuestInfo() {
        String guestString = SharedPreferencesUtils.getString(this, KeyInterface.GuestInfo);
        GuestModel guestModel = JSONObject.parseObject(guestString, GuestModel.class);
        main_guest.setText(guestModel.getCustomer_name());
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private void initView() {
        String userInfo = SharedPreferencesUtils.getString(this, KeyInterface.StaffInfo);
        userStaffInfo = JSONObject.parseObject(userInfo, UserInfo.class);
        main_user_img = findViewById(R.id.main_user_img);
        main_user_name = findViewById(R.id.main_user_name);
        main_user_code = findViewById(R.id.main_user_code);
        main_guest_login = findViewById(R.id.main_guest_login);
        main_callback = findViewById(R.id.main_callback);
        main_logout = findViewById(R.id.main_logout);
        main_guest = findViewById(R.id.main_guest);
        versionName = findViewById(R.id.versionName);
        versionName.setText("V" + AppUtil.returnAppVersionName(this));
        main_user_name.setText(getString(R.string.name, userStaffInfo.getStaff_name()));
        main_user_code.setText(getString(R.string.staff_code, userStaffInfo.getStaff_id()));
        main_guest_login.setOnClickListener(this);
        main_callback.setOnClickListener(this);
        main_logout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.main_guest_login) {
            //顾客登陆
            toGuestLogin();
        } else if (v.getId() == R.id.main_callback) {
            //反馈
            toFeedBack();
        } else if (v.getId() == R.id.main_logout) {
            //退出
            toLogin();
        }
    }

    private void toLogin() {
        Intent intent = new Intent(this, ActivityLogin.class);
        startActivity(intent);
        finish();
    }

    private void toGuestLogin() {
        Intent intent = new Intent(this, ActivityGuestLogin.class);
        startActivity(intent);
    }

    private void toFeedBack() {
        Intent intent = new Intent(this, ActivityCallback.class);
        startActivity(intent);
    }


    @Subscribe
    public void receiveTag(RFID rfid) {
        if (!isTopActivity()) return;
        if (TextUtils.isEmpty(SharedPreferencesUtils.getString(this, KeyInterface.GuestInfo)))
            return;

        if (userStaffInfo.getStaff_number().equals(rfid.getCode())) return;
        if (productRFID.equals(rfid.getCode())) return;
        productRFID = rfid.getCode();
        Bundle bundle = new Bundle();
        bundle.putString("Code", productRFID);
        turnToNextActivity(ActivityProduct.class, bundle);
    }

    private boolean isTopActivity() {
        boolean isTop = false;
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        if (cn.getClassName().contains("ActivityMain")) {
            isTop = true;
        }
        return isTop;
    }
}
